package com.navinfo.tripanalysis.service.impl;

import com.github.yt.excel.exception.ParseExcelException;
import com.navinfo.tripanalysis.util.ExcelUtils;
import com.navinfo.tripanalysis.common.util.DateUtils;
import com.navinfo.tripanalysis.pojo.LoadDataParam;
import com.navinfo.tripanalysis.pojo.Point0200;
import com.navinfo.tripanalysis.pojo.Point;
import com.navinfo.tripanalysis.service.Load0200DataService;
import org.apache.commons.lang3.StringUtils;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.SparkSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 从Excel加载0200数据的服务
 * @author 沈东生
 */
public class Load0200DataServiceExcelImpl implements Load0200DataService, Serializable {
    private static final Logger logger = LoggerFactory.getLogger(Load0200DataServiceExcelImpl.class);
    private String path = "";
    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public JavaRDD<Point> load(SparkSession spark, JavaSparkContext jsc, LoadDataParam param) {
        logger.debug("通过Excel加载0200数据开始...");
        JavaRDD<Point> result = null;
        List<Point> list = new ArrayList<>(1000);

        String dayStr = DateUtils.format(param.getDay(), DateUtils.DateFormat.YYYYMMDD);
        File file = new File(path + File.separator + "0200"+ File.separator + dayStr + File.separator);
        if (file.exists()) {
            Arrays.stream(file.listFiles()).forEach(f ->{
                if (StringUtils.endsWith(f.getName(), "xlsx")) {
                    try {
                        list.addAll(ExcelUtils.readExcel(f, Point0200.class));
                    } catch (ParseExcelException e) {
                        e.printStackTrace();
                    }
                }
            });
            result = jsc.parallelize(list);
        }

        logger.debug("通过Excel加载0200数据结束，大小{}...", list.size());
        return result;
    }
}
